package service;

import PageBean.PageBean;
import mapper.BrandMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class BrandService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<Brand> selectAll() {
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        sqlSession.close();
        return brands;


    }

    public void add(Brand brand){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.add(brand);
        sqlSession.commit();
        sqlSession.close();

    }



    public Brand selectById(int id){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
//调用BrandMapper.selectAll()
//2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        Brand brand = mapper.selectById(id);
        sqlSession.close();
        return brand;
    }

    public void update(Brand brand){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
//调用BrandMapper.selectAll()
//2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper brandmapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        brandmapper.update(brand);
        sqlSession.commit();
        sqlSession.close();

    }

    public void delete(int id){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
//2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
//3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4. 调用方法
        mapper.deleteById(id);
//提交事务
        sqlSession.commit();
//释放资源
        sqlSession.close();
    }

    public PageBean<Brand> selectAllPage(int curPage,int pageSize){
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int beginIndex = (curPage -1 ) * pageSize;
        List<Brand> brandList = brandMapper.selectAllPage(beginIndex ,pageSize);
        int totalCount = brandMapper.selectCount();
        PageBean<Brand> pageBean = new PageBean<Brand>();
        pageBean.setData(brandList);
        pageBean.setCurrentPage(curPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(totalCount);
        int totalPage = (int) Math.ceil( totalCount*1.0/pageSize );
        pageBean.setTotalPage(totalPage);
        sqlSession.close();
        return pageBean;
    }

}
